//
//  LLConditionalView.m
//  TaoTuo
//
//  Created by admin on 2019/8/20.
//  Copyright © 2019 admin. All rights reserved.
//

#import "LLConditionalView.h"
#import "LLUpDownArrowView.h"

@interface LLConditionalView ()

//@property (nonatomic, strong) UILabel *rightLabel;
@property (nonatomic, strong) NSArray *titlesArray;
@property (nonatomic, strong) NSMutableArray *condViewsArray;

//@property (nonatomic, strong) UIButton *changeBtn;  // 网格和列表切换的按钮
@property (nonatomic, strong) UIButton *screeningBtn;    //筛选按钮
@property (nonatomic, strong) UILabel *screeningCountLabel; //筛选按钮上的数字


@end

@implementation LLConditionalView

- (instancetype)initWithFrame:(CGRect)frame titlesArray:(NSArray *)titlesArray totalCount:(int)totalCount
{
    self = [super initWithFrame:frame];
    if (self) {
        
        self.titlesArray = titlesArray;
        
        
        
        
    }
    return self;
}

//- (void)setTotalCount:(int)totalCount
//{
//    _totalCount = totalCount;
//    NSString *rightString = [NSString stringWithFormat:@"共%d款", _totalCount];
//    [self addAttrbuteStringWithString:rightString];
//
//    [self setNeedsLayout];
//
//}

- (void)setDelegate:(id<LLConditionalViewDelegate>)delegate
{
    _delegate = delegate;
    
    [self setupSubviews];
}

//- (void)layoutSubviews
//{
//    [super layoutSubviews];
//
//    CGFloat rightW = [_rightLabel.text boundingRectWithSize:CGSizeMake(MAXFLOAT, self.height) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName : self.rightLabel.font} context:nil].size.width;
//
//    _rightLabel.frame = CGRectMake(self.changeBtn.left - rightW - 6, 0, rightW, self.height);
//
//    CGFloat edgeSpace = 15; // 两边距离
//    CGFloat w = 50;
//    // 中间距离
//    CGFloat space = (self.changeBtn.left - edgeSpace - rightW - self.titlesArray.count * w) / self.titlesArray.count;
//
//    for (int i = 0; i < self.condViewsArray.count; i++) {
//        LLUpDownArrowView *arrowView = self.condViewsArray[i];
//        arrowView.frame = CGRectMake(edgeSpace + i * (w + space), 0, w, self.height);
//    }
//
//}

///筛选按钮点击了
- (void)clickScreeningBtn
{
    if (self.screeningBlock) {
        self.screeningBlock();
    }
}

//- (void)changeBtnAction:(UIButton *)sender
//{
//    sender.selected = !sender.selected;
//    if (self.changeCellTypeBlock) {
//        self.changeCellTypeBlock();
//    }
//}


- (void)setScreeningCount:(int)screeningCount
{
    _screeningCount = screeningCount;
    if (_screeningCount <= 0) {
        self.screeningCountLabel.text = @"0";
        self.screeningCountLabel.hidden = YES;
        return;
    }
    
    self.screeningCountLabel.hidden = NO;
    CGFloat h = 12;
    NSString *countStr = [NSString stringWithFormat:@"%d", _screeningCount];
    CGFloat countW = [countStr boundingRectWithSize:CGSizeMake(MAXFLOAT, 12) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName : self.screeningCountLabel.font} context:nil].size.width;
    countW = countW < h ? h : countW;
    
    self.screeningCountLabel.frame = CGRectMake(_screeningBtn.width - 20, 5, countW + 4, h);
    self.screeningCountLabel.text = countStr;
}

- (void)setHiddenScreening:(BOOL)hiddenScreening
{
    self.screeningBtn.hidden = hiddenScreening;
}

- (void)setupSubviews
{
    // 筛选按钮
    _screeningBtn = [UIButton createButtonWithFrame:CGRectMake(self.width - 60, 0, 60, self.height) image:[UIImage imageNamed:@"shaixuan"] title:@"筛选" titleColor:UIColorHex(666666) titleFont:[UIFont systemFontOfSize:14] space:1 backgroundColor:nil target:self action:@selector(clickScreeningBtn)];
    _screeningBtn.contentEdgeInsets = UIEdgeInsetsMake(0, 0, 0, 10);
    [self addSubview:_screeningBtn];
    
    _screeningCountLabel = [UILabel labelWithTitle:@"" color:[UIColor whiteColor] fontSize:9];
    _screeningCountLabel.textAlignment = NSTextAlignmentCenter;
    _screeningCountLabel.layer.backgroundColor = [UIColor redColor].CGColor;
    _screeningCountLabel.layer.cornerRadius = 6;
    [self.screeningBtn addSubview:_screeningCountLabel];
    
//    _changeBtn = [UIButton createButtonWithNormalImage:[UIImage imageNamed:@"liebiao"] selectedImage:[UIImage imageNamed:@"wangge"] target:self action:@selector(changeBtnAction:)];
//    _changeBtn.frame = CGRectMake(self.width - _screeningBtn.width - self.height, 0, self.height, self.height);
//    _changeBtn.imageView.contentMode = UIViewContentModeCenter;
//    [self addSubview:_changeBtn];
//
//    NSString *rightString = [NSString stringWithFormat:@"共%d款", self.totalCount];
//    _rightLabel = [UILabel labelWithTitle:@"" color:UIColorHex(999999) fontSize:12];
//    _rightLabel.textAlignment = NSTextAlignmentRight;
//    _rightLabel.frame = CGRectMake(self.changeBtn.left - 80, 0, 70, self.height);
//    [self addAttrbuteStringWithString:rightString];
//    [self addSubview:_rightLabel];
    
    
    
    
//    CGFloat rightW = _rightLabel.width;
    CGFloat edgeSpace = 15; // 两边距离
    CGFloat w = 50;
    // 中间距离
//    CGFloat space = (self.changeBtn.left - edgeSpace - rightW - self.titlesArray.count * w) / self.titlesArray.count;
    
    CGFloat space = (self.screeningBtn.left - edgeSpace - self.titlesArray.count * w) / self.titlesArray.count;
    
    [self.condViewsArray removeAllObjects];
    for (int i = 0; i < self.titlesArray.count; i++) {
        NSString *title = self.titlesArray[i];
        @weakify(self)
        LLUpDownArrowView *udArrowView = [LLUpDownArrowView arrowViewWithFrame:CGRectMake(edgeSpace + i * (w + space), 0, w, self.height) title:title selectTypeBlock:^(LLUpDownArrowView * _Nullable arrowView, NSString * _Nullable title, ArrowType arrowType) {
            NSLog(@"---> %@, %lu", title, (unsigned long)arrowType);
            @strongify(self)
            
            if ([self.delegate respondsToSelector:@selector(conditionalView:title:isAsc:)]) {
                [self.delegate conditionalView:self title:title isAsc:arrowType == ArrowTypeWithUp];
            }
            for (LLUpDownArrowView *av in self.condViewsArray) {
                if (av == arrowView) {
                    continue;
                }
                [av resetView];
            }
            
            
        }];
        [self addSubview:udArrowView];
        
//        if (i == 0) {
////            udArrowView.selected = YES;
//            [udArrowView setSelected:YES arrowType:ArrowTypeWithDown];
//        }
        
        [self.condViewsArray addObject:udArrowView];
    }
    
}

- (void)cancelSelected
{
    for (LLUpDownArrowView *udArrowView in self.condViewsArray) {
        [udArrowView resetView];
    }
}

- (void)setSelectItemWithIndex:(NSInteger)index
{
    if (index >= self.titlesArray.count) {
        return;
    }
    LLUpDownArrowView *udArrowView = self.condViewsArray[index];
    [udArrowView setSelected:YES arrowType:ArrowTypeWithDown];
}

//- (void)addAttrbuteStringWithString:(NSString *)text
//{
//    NSString *rangeStr = [NSString stringWithFormat:@"%d", self.totalCount];
//    NSMutableAttributedString *attrStr = [[NSMutableAttributedString alloc] initWithString:text];
//    [attrStr addAttribute:NSForegroundColorAttributeName value:kMainColor range:[text rangeOfString:rangeStr]];
//    self.rightLabel.attributedText = attrStr;
//}


- (NSMutableArray *)condViewsArray
{
    if (!_condViewsArray) {
        _condViewsArray = [NSMutableArray array];
    }
    return _condViewsArray;
}


@end
